package iit.projects.etaxi.view.bean;



import java.util.Date;

import iit.projects.etaxi.model.entity.Customer;
import iit.projects.etaxi.model.service.CustomerService;
import iit.projects.etaxi.model.service.ServiceLocator;
import iit.projects.etaxi.util.Constants;
import iit.projects.etaxi.util.JSFUtils;

import javax.faces.component.UIInput;
import javax.faces.event.ActionEvent;

import com.icesoft.faces.component.ext.HtmlInputSecret;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlInputTextarea;
import com.icesoft.faces.component.ext.HtmlSelectOneRadio;
import com.icesoft.faces.component.selectinputdate.SelectInputDate;

public class CustomerBackingBean {
	
	private Customer customer;
	private Customer initialCustomer;
	private boolean editMode;
	
	private HtmlInputText compFName;
	private HtmlInputText compLName;
	private HtmlInputTextarea compRAddress;
	private HtmlInputTextarea compOAddress;
	private HtmlInputText compMContact;
	private HtmlInputText compHContact;
	private HtmlInputText compUName;
	private HtmlSelectOneRadio compGender;
	private HtmlInputText compEmail;
	private HtmlInputSecret compPassword;
	private HtmlInputSecret compRetypePassword;
	private HtmlInputText compCardNo;
	private HtmlInputText compCvc;
	private HtmlInputText compCardName;
	private HtmlSelectOneRadio compCardType;
	private SelectInputDate compExpiryDate;
	
	private CustomerService customerService;
	private ServiceLocator serviceLocator;
	
	private Date timeNow;
	
	public CustomerBackingBean() {
	}
	
	@javax.annotation.PostConstruct
	public void init(){
		customerService = serviceLocator.getCustomerService();
		
		String userName = (String)JSFUtils.getFromSession(Constants.USERNAME);
		if(userName == null){
			editMode = false;
			this.initialCustomer = new Customer();
			this.initialCustomer.setCardType(Constants.DEFAULT_CREDIT_CARD);
			this.initialCustomer.setGender(Constants.DEFAULT_GENDER);
		}
		else{
			editMode = true;
			try{
				this.initialCustomer = customerService.findByUsername(userName);
			}catch(Throwable e){
				return;
			}
		}
		this.customer = new Customer(this.initialCustomer);
	}
	
	public void saveCustomer(ActionEvent event){
		if(!validCustomer()){
			return;
		}
		try {
			customerService.saveCustomer(this.customer);
			JSFUtils.addFacesInfoMessage("customer.registration.successfull");
		} catch (Throwable e) {
			return;
		}
		this.clear(event);
	}
	
	public void updateCustomer(ActionEvent event){
		try{
			this.initialCustomer = customerService.updateCustomer(this.customer);
			clear(event);
			JSFUtils.addFacesInfoMessage("customer.successfully.saved");
		}catch(Throwable e){
			return;
		}
	}
	
	public void clear(ActionEvent event){
		this.customer = new Customer(this.initialCustomer);
		
		compFName.resetValue();
		compLName.resetValue();
		compRAddress.resetValue();
		compOAddress.resetValue();
		compMContact.resetValue();
		compHContact.resetValue();
		compGender.resetValue();
		compEmail.resetValue();
		compUName.resetValue();
		if(!editMode){
			compPassword.resetValue();
			compRetypePassword.resetValue();
			compCardNo.resetValue();
			compCardName.resetValue();
			compCvc.resetValue();
			compCardType.resetValue();
			compExpiryDate.resetValue();
		}
	}
	
	private boolean validCustomer(){
		
		boolean valid = true;
		timeNow = new Date();
		
		if(customer.getFirstName() == null || customer.getFirstName().isEmpty()){
			JSFUtils.addFacesErrorMessage(compFName, UIInput.REQUIRED_MESSAGE_ID);
			valid = false;
		}
		
		if(customer.getLastName() == null || customer.getLastName().isEmpty()){
			JSFUtils.addFacesErrorMessage(compLName, UIInput.REQUIRED_MESSAGE_ID);
			valid = false;
		}
		
		if(customer.getEmail() == null || customer.getEmail().isEmpty()){
			JSFUtils.addFacesErrorMessage(compEmail, UIInput.REQUIRED_MESSAGE_ID);
			valid = false;
		}
		
		if(customer.getUserName() == null || customer.getUserName().isEmpty()){
			JSFUtils.addFacesErrorMessage(compUName, UIInput.REQUIRED_MESSAGE_ID);
			valid = false;
		}
		
		if(customer.getPassword() == null || customer.getPassword().isEmpty()){
			JSFUtils.addFacesErrorMessage(compPassword, UIInput.REQUIRED_MESSAGE_ID);
			valid = false;
		}
		
		String passConfirm = (String)compRetypePassword.getValue();
		if(passConfirm == null || passConfirm.isEmpty()){
			JSFUtils.addFacesErrorMessage(compRetypePassword, UIInput.REQUIRED_MESSAGE_ID);
			valid = false;
		}
		else if(!passConfirm.equals(customer.getPassword())){
			JSFUtils.addFacesErrorMessage(compRetypePassword, "passwords.doesnotmatch");
			valid = false;
		}
		
		if(customer.getCreditCardNumber() == null){
			JSFUtils.addFacesErrorMessage(compCardNo, UIInput.REQUIRED_MESSAGE_ID);
			valid = false;
		}
		
		if(customer.getCardName() == null || customer.getCardName().isEmpty()){
			JSFUtils.addFacesErrorMessage(compCardName, UIInput.REQUIRED_MESSAGE_ID);
			valid = false;
		}
		
		if(customer.getCvc() == null){
			JSFUtils.addFacesErrorMessage(compCvc, UIInput.REQUIRED_MESSAGE_ID);
			valid = false;
		}
		
		if(customer.getExpiryDate() == null){
			JSFUtils.addFacesErrorMessage(compExpiryDate, UIInput.REQUIRED_MESSAGE_ID);
			valid = false;
		}
		else
		{
			if(customer.getExpiryDate().before(timeNow)){
				JSFUtils.addFacesErrorMessage(compExpiryDate, "customer.registration.CreditCardExpirationDate");
				valid = false;
			}
		}
		
		
		
		return valid;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public Customer getInitialCustomer() {
		return initialCustomer;
	}

	public void setInitialCustomer(Customer initialCustomer) {
		this.initialCustomer = initialCustomer;
	}

	public HtmlInputText getCompFName() {
		return compFName;
	}

	public void setCompFName(HtmlInputText compFName) {
		this.compFName = compFName;
	}

	public HtmlInputText getCompLName() {
		return compLName;
	}

	public void setCompLName(HtmlInputText compFLame) {
		this.compLName = compFLame;
	}

	public HtmlInputTextarea getCompRAddress() {
		return compRAddress;
	}

	public void setRCompAddress(HtmlInputTextarea compAddress) {
		this.compRAddress = compAddress;
	}

	public HtmlInputText getCompMContact() {
		return compMContact;
	}

	public void setCompMContact(HtmlInputText compMContact) {
		this.compMContact = compMContact;
	}

	public void setServiceLocator(ServiceLocator serviceLocator) {
		this.serviceLocator = serviceLocator;
	}

	public HtmlInputText getCompUName() {
		return compUName;
	}

	public void setCompUName(HtmlInputText compUName) {
		this.compUName = compUName;
	}

	public HtmlInputTextarea getCompOAddress() {
		return compOAddress;
	}

	public void setCompOAddress(HtmlInputTextarea compOAddress) {
		this.compOAddress = compOAddress;
	}

	public HtmlInputText getCompHContact() {
		return compHContact;
	}

	public void setCompHContact(HtmlInputText compHContact) {
		this.compHContact = compHContact;
	}

	public HtmlInputText getCompEmail() {
		return compEmail;
	}

	public void setCompEmail(HtmlInputText compEmail) {
		this.compEmail = compEmail;
	}

	public HtmlInputSecret getCompPassword() {
		return compPassword;
	}

	public void setCompPassword(HtmlInputSecret compPassword) {
		this.compPassword = compPassword;
	}

	public HtmlInputSecret getCompRetypePassword() {
		return compRetypePassword;
	}

	public void setCompRetypePassword(HtmlInputSecret compRetypePassword) {
		this.compRetypePassword = compRetypePassword;
	}

	public void setCompRAddress(HtmlInputTextarea compRAddress) {
		this.compRAddress = compRAddress;
	}

	public HtmlSelectOneRadio getCompGender() {
		return compGender;
	}

	public void setCompGender(HtmlSelectOneRadio compGender) {
		this.compGender = compGender;
	}

	public HtmlInputText getCompCardNo() {
		return compCardNo;
	}

	public void setCompCardNo(HtmlInputText compCardNo) {
		this.compCardNo = compCardNo;
	}

	public HtmlInputText getCompCvc() {
		return compCvc;
	}

	public void setCompCvc(HtmlInputText compCvc) {
		this.compCvc = compCvc;
	}

	public HtmlInputText getCompCardName() {
		return compCardName;
	}

	public void setCompCardName(HtmlInputText compCardName) {
		this.compCardName = compCardName;
	}

	public HtmlSelectOneRadio getCompCardType() {
		return compCardType;
	}

	public void setCompCardType(HtmlSelectOneRadio compCardType) {
		this.compCardType = compCardType;
	}

	public SelectInputDate getCompExpiryDate() {
		return compExpiryDate;
	}

	public void setCompExpiryDate(SelectInputDate compExpiryDate) {
		this.compExpiryDate = compExpiryDate;
	}

	public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean editMode) {
		this.editMode = editMode;
	}
}
